//
//  WCMainSubCell.m
//  CBT
//
//  Created by apple on 2019/7/9.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "WCMainSubCell.h"

@interface WCMainSubCell()

@property (nonatomic, strong) UIView *rootView;
@property (nonatomic, strong) UIImageView *rootLayerView;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UIImageView *roundIconImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UILabel *detailMoneyLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *rechargeBtn;
@property (nonatomic, strong) UIButton *transferBtn;
@property (nonatomic, strong) UIButton *investmentBtn;

@end

@implementation WCMainSubCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:@"mainsubCell"]) {
        [self initSubViews];
    }
    
    return self;
}

-(void)setModel:(CoinInfoItemModel *)model
{
    _model = model;
    [_rechargeBtn setTitle:kLocalizedString(@"充值", nil) forState:(UIControlStateNormal)];
    [_investmentBtn setTitle:kLocalizedString(@"兑换", nil) forState:(UIControlStateNormal)];
    [_transferBtn setTitle:kLocalizedString(@"转账", nil) forState:(UIControlStateNormal)];
    [self.iconImageView lf_setImageURL:[NSString stringWithFormat:@"%@/%@",kBaseURL_PICSTATIC,model.coinPic]];
    self.nameLabel.text  = model.coinSymbol;
    self.moneyLabel.text = [self judgeStringForDecimalPlaces:[model.coinAmount stringValue] point:4];
    self.detailMoneyLabel.text = [NSString stringWithFormat:@"≈￥ %.2f",[model.coinPrice floatValue]];
}

- (void)initSubViews
{
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle  = UITableViewCellSelectionStyleNone;
    
    _rootView = [[UIView alloc]init];
    _rootView.userInteractionEnabled = YES;
    _rootView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_rootView];
    
    _rootLayerView = [[UIImageView alloc]init];
    _rootLayerView.image = [UIImage resizableImageWithName:@"huijiao"];
    [self.contentView addSubview:_rootLayerView];
    
    _iconImageView = [[UIImageView alloc]init];
    _iconImageView.layer.masksToBounds = YES;
    _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    _iconImageView.backgroundColor = DefaultImageViewBgColor;
    [_rootView addSubview:_iconImageView];
    
    _roundIconImageView = [[UIImageView alloc]init];
    _roundIconImageView.image = [UIImage imageNamed:@"white_bg"];
    [_rootView addSubview:_roundIconImageView];
    
    _moneyLabel = [[UILabel alloc]init];
    _moneyLabel.font = [UIFont systemFontOfSize:16];
    _moneyLabel.textColor = UIColorFromRGB(0x333333);
    [_rootView addSubview:_moneyLabel];
    
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.text = @"0x";
    _nameLabel.font = [UIFont systemFontOfSize:18];
    _nameLabel.textColor = UIColorFromRGB(0x333333);
    [_rootView addSubview:_nameLabel];
    
    _detailMoneyLabel = [[UILabel alloc]init];
    _detailMoneyLabel.text = @"--";
    _detailMoneyLabel.font = [UIFont systemFontOfSize:12];
    _detailMoneyLabel.textColor = UIColorFromRGB(0x919191);
    [_rootView addSubview:_detailMoneyLabel];
    
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = LineViewBgColor;
    [_rootView addSubview:_lineView];
    
    _rechargeBtn = [[UIButton alloc]init];
    [_rechargeBtn setTitleColor:UIColorFromRGB(0x666666) forState:(UIControlStateNormal)];
    _rechargeBtn.layer.cornerRadius = 5;
    [_rechargeBtn addTarget:self action:@selector(rechargeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _rechargeBtn.layer.borderWidth = 1;
    _rechargeBtn.layer.borderColor = LineViewBgColor.CGColor;
    [_rootView addSubview:_rechargeBtn];
    
    _transferBtn = [[UIButton alloc]init];
    [_transferBtn setTitleColor:UIColorFromRGB(0x666666) forState:(UIControlStateNormal)];
    _transferBtn.layer.cornerRadius = 5;
    [_transferBtn addTarget:self action:@selector(transferBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _transferBtn.layer.borderWidth = 1;
    _transferBtn.layer.borderColor = LineViewBgColor.CGColor;
    [_rootView addSubview:_transferBtn];
    
    _investmentBtn = [[UIButton alloc]init];
    [_investmentBtn setTitleColor:UIColorFromRGB(0x666666) forState:(UIControlStateNormal)];
    _investmentBtn.layer.cornerRadius = 5;
    _investmentBtn.layer.borderWidth  = 1;
    [_investmentBtn addTarget:self action:@selector(investmentBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _investmentBtn.layer.borderColor = LineViewBgColor.CGColor;
    [_rootView addSubview:_investmentBtn];
    
    [self controlLayout];
    
}

- (void)transferBtnAction:(UIButton *)btn {
    
    if (self.mainCellBlock != nil) {
        self.mainCellBlock(2,self.model);
    }
}

- (void)rechargeBtnAction:(UIButton *)btn
{
    if (self.mainCellBlock != nil) {
        self.mainCellBlock(0,self.model);
    }
}

- (void)investmentBtnAction:(UIButton *)btn
{
    if (self.mainCellBlock != nil) {
        self.mainCellBlock(1,self.model);
    }
}

- (void)controlLayout
{
    __weak typeof(self) weakSelf = self;
    [_rootView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(5);
        make.height.mas_equalTo(130);
    }];
    
    [_rootLayerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(5);
        make.height.mas_equalTo(130);
    }];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(35);
        make.left.top.mas_equalTo(15);
    }];
    
    [_roundIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(35);
        make.left.top.mas_equalTo(15);
    }];
    
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(16);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.roundIconImageView.mas_right).offset(15);
        make.top.mas_equalTo(26);
        make.height.mas_equalTo(18);
        make.right.lessThanOrEqualTo(weakSelf.moneyLabel.mas_left).offset(-10);
    }];
    
    [_detailMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(weakSelf.moneyLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(12);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(weakSelf.roundIconImageView.mas_bottom).offset(18);
        make.height.mas_equalTo(1);
    }];
    
    [@[_rechargeBtn, _transferBtn, _investmentBtn] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:8 leadSpacing:8 tailSpacing:8];
    
    [@[_rechargeBtn, _transferBtn, _investmentBtn] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.lineView.mas_bottom).offset(15);
        make.height.mas_equalTo(32);
    }];
}

//MARK:--判断字符串后有几位小数，超过八位就省略
-(NSString *)judgeStringForDecimalPlaces:(NSString *)string point:(NSInteger)point {
    NSString *numStr = @"";
    NSArray *array = [string componentsSeparatedByString:@"."];
    if (array.count == 2) {
        NSString *str = array[1];
        if (str.length > point) {
            numStr = [NSString stringWithFormat:@"%@.%@",array[0],[str substringWithRange:NSMakeRange(0, point)]];
        }else{
            numStr = string;
        }
    }else{
        numStr = string;
    }
    return numStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
